package com.foursome.shouguang.controller;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.foursome.shouguang.common.R;
import com.foursome.shouguang.entity.Orders;
import com.foursome.shouguang.entity.Stock;
import com.foursome.shouguang.service.OrderService;
import com.foursome.shouguang.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private StockService stockService;

    @PostMapping
    @CrossOrigin(allowCredentials = "true",allowedHeaders = "*", originPatterns ="*")
    public R<String> save(@RequestBody Orders order){

        if (order.getOrderAccount()<=0) return R.error("购买数量错误！");
        log.info("新增订单，订单信息：{}",order.toString());

        order.setOrderTime(LocalDateTime.now());
        orderService.save(order);

        Integer id = order.getStockId();
        Stock stock = stockService.getById(id);

        Integer raw = stock.getStockSum();
        Integer reduce = order.getOrderAccount();
        log.info(String.valueOf(raw));
        stock.setStockSum(raw - reduce);
        stockService.updateById(stock);

        return R.success("新增订单成功");
    }

    @GetMapping("/OnlineList")
    @CrossOrigin(allowCredentials = "true",allowedHeaders = "*", originPatterns ="*")
    public R<Page> getOnlineList(String orderObtain){

        //构造分页构造器
        Page pageInfo = new Page();

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(StringUtils.isNotEmpty(orderObtain),Orders::getOrderObtain,orderObtain);
        queryWrapper.eq(Orders::getOrderWay,"线上");
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);

        //执行查询
        orderService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @GetMapping("/OfflineList")
    @CrossOrigin(allowCredentials = "true",allowedHeaders = "*", originPatterns ="*")
    public R<Page> getOfflineList(String orderObtain){

        //构造分页构造器
        Page pageInfo = new Page();

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(StringUtils.isNotEmpty(orderObtain),Orders::getOrderObtain,orderObtain);
        queryWrapper.eq(Orders::getOrderWay,"线下");
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);

        //执行查询
        orderService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


}
